package com.yanqu.dao;

import com.yanqu.data.entity.HistoryGoodsGetData;
import com.yanqu.jdbc.JdbcTemplateHelper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HistoryGoodsGetDataDaoImpl extends TempDataObjectDao<HistoryGoodsGetData> {

    public long getMaxHistoryId() {
        String sql = "select ifnull(max(dbId),0) as maxId from t_m_history_get_goods ";
        long maxId = 0;
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(sql);
            if(sqlRowSet.next()) {
                maxId = sqlRowSet.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return maxId;
    }

    public static String getHistoryGoodsGetDataSql = "select * from `t_m_history_get_goods` where `dayTime` > ?";
    public Map<Integer, Map<Integer, Map<Integer, List<HistoryGoodsGetData>>>> getHistoryGoodsGetData(int dayTime) {
        Map<Integer, Map<Integer, Map<Integer, List<HistoryGoodsGetData>>>> result = new ConcurrentHashMap<>();
        try {
            int[] types = new int[]{Types.INTEGER};
            Object[] values = new Object[]{dayTime};
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(getHistoryGoodsGetDataSql, values, types);
            while (sqlRowSet.next()) {
                HistoryGoodsGetData data = parse(sqlRowSet);
                if(!result.containsKey(data.getDayTime())){
                    result.put(data.getDayTime(), new ConcurrentHashMap<>());
                }
                if(!result.get(data.getDayTime()).containsKey(data.getVip())){
                    result.get(data.getDayTime()).put(data.getVip(), new ConcurrentHashMap<>());
                }
                if(!result.get(data.getDayTime()).get(data.getVip()).containsKey(data.getWay())){
                    result.get(data.getDayTime()).get(data.getVip()).put(data.getWay(), new ArrayList<>());
                }
                result.get(data.getDayTime()).get(data.getVip()).get(data.getWay()).add(data);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return result;
    }

    public static String updateHistoryGoodsGetDataSql = "update `t_m_history_get_goods` set dayTime=?,vip=?,way=?,goodsId=?,maxNum=?,maxTimes=?,maxNumUser=?,maxTimesUser=? where dbId=?";
    @Override
    protected boolean updateData(HistoryGoodsGetData historyGoodsGetData) {
        historyGoodsGetData.setBeginWriteTime();
        int[] types = new int[]{Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT};
        Object[] values = new Object[9];
        int i = 0;
        values[i++] = historyGoodsGetData.getDayTime();
        values[i++] = historyGoodsGetData.getVip();
        values[i++] = historyGoodsGetData.getWay();
        values[i++] = historyGoodsGetData.getGoodsId();
        values[i++] = historyGoodsGetData.getMaxNum();
        values[i++] = historyGoodsGetData.getMaxTimes();
        values[i++] = historyGoodsGetData.getMaxNumUser();
        values[i++] = historyGoodsGetData.getMaxTimesUser();
        values[i++] = historyGoodsGetData.getDbId();
        if (JdbcTemplateHelper.getJdbcTemplate().update(updateHistoryGoodsGetDataSql, values, types) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHistoryGoodsGetDataSql = "insert into `t_m_history_get_goods`(dbId,dayTime,vip,way,goodsId,maxNum,maxTimes,maxNumUser,maxTimesUser) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HistoryGoodsGetData historyGoodsGetData) {
        historyGoodsGetData.setBeginWriteTime();
        int[] types = new int[]{Types.BIGINT, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT};
        Object[] values = new Object[9];
        int i = 0;
        values[i++] = historyGoodsGetData.getDbId();
        values[i++] = historyGoodsGetData.getDayTime();
        values[i++] = historyGoodsGetData.getVip();
        values[i++] = historyGoodsGetData.getWay();
        values[i++] = historyGoodsGetData.getGoodsId();
        values[i++] = historyGoodsGetData.getMaxNum();
        values[i++] = historyGoodsGetData.getMaxTimes();
        values[i++] = historyGoodsGetData.getMaxNumUser();
        values[i++] = historyGoodsGetData.getMaxTimesUser();
        if (JdbcTemplateHelper.getJdbcTemplate().update(insertHistoryGoodsGetDataSql, values, types) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HistoryGoodsGetData parseData(SqlRowSet rs) throws SQLException {
        HistoryGoodsGetData historyGoodsGetData = new HistoryGoodsGetData();
        historyGoodsGetData.setDbId(rs.getLong("dbId"));
        historyGoodsGetData.setDayTime(rs.getInt("dayTime"));
        historyGoodsGetData.setVip(rs.getInt("vip"));
        historyGoodsGetData.setWay(rs.getInt("way"));
        historyGoodsGetData.setGoodsId(rs.getInt("goodsId"));
        historyGoodsGetData.setMaxNum(rs.getLong("maxNum"));
        historyGoodsGetData.setMaxTimes(rs.getLong("maxTimes"));
        historyGoodsGetData.setMaxNumUser(rs.getLong("maxNumUser"));
        historyGoodsGetData.setMaxTimesUser(rs.getLong("maxTimesUser"));

        return historyGoodsGetData;
    }
}
